#!/bin/ksh

#--------------------------------------------------------------------------
#
# LINUX Only.
#
# Main script to configure websm.  Typically called from an rpm package
#
#--------------------------------------------------------------------------

USER_SETTINGS="/var/websm/config/user_settings/websm.cfg"
FACTORY_DEFAULTS="/usr/websm/config/factory_defaults/websm.cfg"

# Check whether we need to do security specific work

if [ $# -gt 0 ] ; then

  if [ "$1" = "Security" ] ; then
     mkdir -p /var/websm/security
     exit $?
  fi
  exit 
fi 

# Add websm to the inittab
if [ -f /usr/websm/bin/rc.websm ]; then
   /usr/websm/bin/rc.websm -enable
fi

# Configure websm to work with pam
if [ -f /usr/websm/bin/wsmpamconfig ]; then
   /usr/websm/bin/wsmpamconfig -enable
fi

if [ -f /usr/websm/bin/wsmserver ]; then
   /usr/websm/bin/wsmserver -enable
fi

#
# Symbolic links needed
#
if [ -d /usr/websm/html ] ; then
   ln -s /usr/websm                        /usr/websm/html/wsmship
   ln -s /usr/websm/codebase               /usr/websm/html/codebase
fi

# Add websm and related directories in var 
if [ ! -d /var/websm/config/user_settings ]; then
   mkdir -p /var/websm/config/user_settings
fi

# Add directory for addition jars
if [ ! -d /usr/websm/codebase/pluginjars ]; then
   mkdir -p /usr/websm/codebase/pluginjars
fi

#ln -s /usr/lib/java/taskguide/rsd       /usr/websm/codebase
#ln -s /usr/lib/java/taskguide/sguide    /usr/websm/codebase
#ln -s /usr/lib/java/taskguide           /usr/websm/codebase
ln -s /usr/websm/codebase/taskguide/sguide    /usr/websm/codebase
ln -s /var/websm/config/user_settings/websm.cfg /usr/websm/websm.cfg
ln -s /websm.script                     /var/websm/websm.script
ln -s /usr/websm/bin/wsmoutput          /usr/websm/bin/wsmrefresh
ln -s /usr/websm/bin/configassist       /usr/bin/configassist
ln -s /usr/websm/bin/wsm                /usr/bin/wsm

if [  -d ./opt -a ! -f ./opt/websm ] ; then
   ln -s /usr/websm /opt/websm
fi



# 
# Preserve user settings if there are any, such that lines
# in user_settings take precedence over factory defaults
# 

if [[ -f $USER_SETTINGS ]] ; then

      # Save forcessl setting.  If not set, use forcessl=false
      FORCESSL=$(cat $USER_SETTINGS | /bin/grep "forcessl" | grep -v "#") 2>/dev/null  
      FORCESSL=${FORCESSL:-forcessl=false}

      # Get user_settings contents except forcessl and original comments
      /bin/cat $USER_SETTINGS | /bin/grep -v "forcessl"  \
	 | /bin/grep -v "#######"                        \
         | /bin/grep -v "# The following"                \
         | /bin/grep -v "# and user_settings"            \
                > /tmp/wcfg_user_settings.$$  2>/dev/null

      # Get factory_defaults contents except forcessl and comments
      /bin/cat $FACTORY_DEFAULTS | /bin/grep -v "forcessl" \
         | /bin/grep -v "#" > /tmp/wcfg_factory_defs.$$  2>/dev/null

      # Get lines in factory_defaults that are not over-ridden in user_settings
      cat /tmp/wcfg_factory_defs.$$ | while read line
	do
	/bin/grep -q $line /tmp/wcfg_user_settings.$$ || echo $line >> /tmp/wcfg_fact_def_only.$$  2>/dev/null
	done


      # Overwrite user settings with combination of saved user_settings
      # and factory_defaults
      # Start with this big comment 

      /bin/echo "#################################################################" > $USER_SETTINGS

      /bin/echo "# The following entries are created by merging factory_defaults" >> $USER_SETTINGS

      /bin/echo "# and user_settings files as a part of post installation of WebSM." >> $USER_SETTINGS

      /bin/echo "#################################################################" >> $USER_SETTINGS

      # Add the lines that were unique to factory defaults 
      /bin/cat /tmp/wcfg_fact_def_only.$$ >> $USER_SETTINGS  2>/dev/null

      # Add saved user settings (except forcessl)
      /bin/cat /tmp/wcfg_user_settings.$$ >> $USER_SETTINGS  2>/dev/null

      # Add saved forcessl setting 
      /bin/echo $FORCESSL >> $USER_SETTINGS

else

      # If /var/websm/config/user_settings/websm.cfg does not exist,
      # check for a saved copy else just copy from factory_defaults

      if [[ -f /usr/websm/websm.cfg.saved ]] ; then
          /usr/bin/cp /usr/websm/websm.cfg.saved                            \
          /var/websm/config/user_settings/websm.cfg > /dev/null 2>&1
      else 

          /bin/cp -p /usr/websm/config/factory_defaults/websm.cfg   \
          /var/websm/config/user_settings/websm.cfg
      fi
fi

# clean up temporary files
/bin/rm -f /tmp/wcfg_user_settings.$$
/bin/rm -f /tmp/wcfg_factory_defs.$$
/bin/rm -f /tmp/wcfg_fact_def_only.$$



